package com.winchain.provider;

import com.shell.bee.base.utils.StringUtil;
import com.shell.bee.base.utils.SystemUtil;
import com.shell.bee.base.utils.SystemUtil.SystemMode;
import com.shell.feign.PubParameterFeign;
import com.shell.framework.utils.SysDateUtil;
import com.shell.governor.modules.vo.ParameterVo;
import com.winchain.chainbill.bill.base.po.CbDctQt;
import com.winchain.chainbill.bill.base.service.CbDctQtService;
import java.text.ParseException;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class DctQtExpProvider {
	protected final Log logger = LogFactory.getLog(getClass());
	@Autowired
	private CbDctQtService cbDctQtService;
	@Autowired
	private PubParameterFeign pubParameterFeign;

	@RequestMapping({ "/dctQtExp/expire" })
	public void expire(@RequestParam("date") String date) {
		try {
			String workDate = getWorkDate(date);
			CbDctQt dctQt = new CbDctQt();
			dctQt.setExpDate(workDate);
			List<CbDctQt> dctQts = this.cbDctQtService.selectByEntity(dctQt);
			for (CbDctQt expDctQt : dctQts) {
				expDctQt.setQtState("1");
				this.cbDctQtService.updateNotNull(expDctQt);
			}
		} catch (Exception e) {
			e.printStackTrace();
			this.logger.error("������������������" + e.getMessage());
		}
	}

	private String getWorkDate(String workDate) throws ParseException {
		if (!SystemUtil.SystemMode.prod.equals(SystemUtil.getSystemMode())) {
			ParameterVo parameter = this.pubParameterFeign.getCommonParameter("SYS_DATE_CONF");
			if ((parameter != null) && (!StringUtil.emptyAndNull(parameter.getParmValue()))) {
				workDate = parameter.getParmValue();
			}
		}
		workDate = SysDateUtil.getPreDay(workDate, -1);
		return workDate;
	}
}
